package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.AfterSaleReturnVisit;
import com.sl.au.entity.Attachment;
import com.sl.au.entity.WholeSetDevice;
import com.sl.au.service.AfterSaleReturnVisitService;
import com.sl.au.service.AttachmentService;
import com.sl.au.service.OptionService;
import com.sl.au.service.SaleAreaService;


/*
 * 售后电话回访
 */

@RestController
@RequestMapping(value = "/ui/aftersalereturnvisit")
public class AfterSaleReturnVisitController extends BaseController {

	@Autowired
	AfterSaleReturnVisitService AfterSaleReturnVisitService;
	
	@Autowired
	AttachmentService attachmentService;
	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("aftersalereturnvisit/returnvisitindex");
		return modelAndView;
	}
	
	@RequestMapping(value="getpage",method = RequestMethod.GET)
	public Map<String, Object> getServicePage(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");

		List<AfterSaleReturnVisit> afterSaleReturnVisitService=null;
		if(org.springframework.util.StringUtils.hasText(key)){
			afterSaleReturnVisitService = AfterSaleReturnVisitService.getAfterSaleReturnVisit(start, length, key, null, "revisitTime");}
		else {
			afterSaleReturnVisitService =AfterSaleReturnVisitService.getPage(start, length, null, null, "createTime");
		}
		Long count = AfterSaleReturnVisitService.getRepository().count();
		List<AfterSaleReturnVisit> newAfterSaleReturnVisitService = new ArrayList<AfterSaleReturnVisit>();
		for (AfterSaleReturnVisit r : afterSaleReturnVisitService) {
			String parentId = r.getId();
			List<Attachment> a = attachmentService.getRepository().findByParentId(parentId);
			int num = a.size();
			r.setAttachNumber(num);
			newAfterSaleReturnVisitService.add(r);
		}
		map.put("data", newAfterSaleReturnVisitService);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
    ////打开新增页面
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addVisit(HttpServletRequest request) {
		ModelAndView mv = createLayoutView("aftersalereturnvisit/returnvisitadd");
		
		return mv;
	}
	//编辑
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView editVisit(HttpServletRequest request) {
		ModelAndView mv = createLayoutView("aftersalereturnvisit/returnvisitedit");
		String id=request.getParameter("id");
		if(id!=null){
			AfterSaleReturnVisit afterSaleReturnVisit=AfterSaleReturnVisitService.getRepository().findById(id);
			mv.addObject("afterSaleReturnVisit",afterSaleReturnVisit);
		}

		return mv;
	}
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Map<String, Object> saveAfterSaleReturnVisit(HttpServletRequest request, @RequestBody AfterSaleReturnVisit afterSaleReturnVisit) {
		Map<String, Object> map = new HashMap<String, Object>();
		AfterSaleReturnVisitService.getRepository().save(afterSaleReturnVisit);
		map.put("afterSaleReturnVisit", afterSaleReturnVisit);
		return map;
	}
	///删除
	@RequestMapping(value = "del", method = RequestMethod.GET)
	public Map<String, Object> delAfterSaleReturnVisit(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		String ids=request.getParameter("ids");
		String id[]=ids.split(",");
		List<AfterSaleReturnVisit> afterSaleReturnVisits=new ArrayList<AfterSaleReturnVisit>();
		for(int i=0;i<id.length;i++){
			String afterSaleReturnVisitId = id[i];
			AfterSaleReturnVisit AfterSaleReturnVisit=new AfterSaleReturnVisit();
			List<Attachment> a = attachmentService.getRepository().findByParentId(afterSaleReturnVisitId);
			attachmentService.getRepository().delete(a);
			AfterSaleReturnVisit.setId(afterSaleReturnVisitId);
			afterSaleReturnVisits.add(AfterSaleReturnVisit);
		}
		AfterSaleReturnVisitService.getRepository().deleteInBatch(afterSaleReturnVisits);
		map.put("code", 0);
		return map;
	}
}
